/**
 * @QueueProxyAction.as
 * @author soda.C mail:asframe@qq.com
 * @version  1.0
 * <br>Copyright (C), 2008 soda.C
 * <br>This program is protected by copyright laws.
 * <br>Program Name:Core
 * @data 2008-6-15
 */
package com.asframe.action
{
	/**
	 * 队列动作代理器，代理所有类型为ActionConstants.QUEUE类型的动作,进行队列播放<br>
	 * 当代理器里的所有动作都播放完时，则该代理器从动作管理器中删除
	 * 
	 * @author soda.C
	 */ 
	public class QueueActionProxy extends BasicAction
	{
		/** 队列里的action **/
		private var actions		:Vector.<IAction>;
		/** 当前执行的动作 **/
		private var currAction	:IAction;
		
		public function QueueActionProxy()
		{
			actions = new Vector.<IAction>();
			this.mType = ActionConsts.QUEUE;
			this.mName = "QueueProxyAction";
		}
		
		public override function destroy(o:* = null):void
		{
			if(currAction != null)
			{
				overAction(currAction);
				currAction = null;
			}
			if(actions != null && actions.length > 0)
			{
				for(var i:int = 0; i < actions.length; i++)
				{
					overAction(actions[i]);
				}
			}
			actions = null;
		}
		protected override function onStart():void
		{
			//直接选择一个action进行处理
			actAction();
		}
		/**
		 * 结束一个action，同时会调用action的overAction方法
		 * @param action
		 * 
		 */		
		private function overAction(action:IAction):void
		{
			if(action != null)
			{
				try
				{
					action.destroy();
				}
				catch(e:Error)
				{
					trace(action + "强行结束出错:" + e.getStackTrace());
				}
			}
		}
		/**
		 * 添加一个新的action 
		 * @param action
		 * 
		 */		
		public function addAction(action:IAction):void
		{
			actions.push(action);
		}
		/**
		 * 选择一个新的action来处理 
		 */
		public function actAction():void
		{
			if(actions.length == 0)
			{
				destroy();
			}
			//更新新的动作
			currAction = actions.shift();
			if(currAction == null)
			{
				//出现空的，继续选择
				actAction();
			}
			else
			{
				try
				{
					currAction.start(mActor);
				}
				catch(error:Error)
				{
					trace("代理器执行currAction.start出错. currAction" + currAction);
					trace("错误打印:" + error.getStackTrace());
					//执行错误，强制关闭，继续选择新的
					overAction(currAction);
					actAction();
				}
			}
		}
	}
}
